package com.bee.littlebeetravel.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.config.AppStatus;
import com.bee.littlebeetravel.utils.AppStatusManager;

import net.grandcentrix.tray.AppPreferences;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_splash);
        setStatusBarStyle("1");

        AppPreferences appPreferences = new AppPreferences(this);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String isLoginSuccess = appPreferences.getString("isLoginSuccess", "0");
                if (isLoginSuccess.equals("0")) {
                    Intent intent = new Intent(getApplicationContext(), LoginIndexActivity.class);
                    //app状态改为正常
                    AppStatusManager.getInstance().setAppStatus(AppStatus.STATUS_NORMAL);
                    startActivity(intent);
                    finish();
                } else if (isLoginSuccess.equals("1")) {
                    Intent intent = new Intent(getApplicationContext(), LoginIndexActivity.class);
                    //app状态改为正常
                    AppStatusManager.getInstance().setAppStatus(AppStatus.STATUS_NORMAL);
                    startActivity(intent);
                    finish();
                }
            }
        }, 2000);
    }


    /**
     * 设置状态背景和颜色
     * 1.白底黑字
     * 2.黑底白字
     *
     * @param type
     */
    public void setStatusBarStyle(String type) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (type.equals("1")) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            } else if (type.equals("2")) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//设置状态栏白色字体
            }


        }
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            if (type.equals("1")) {
                window.setStatusBarColor(getResources().getColor(R.color.white));
            } else if (type.equals("2")) {
                window.setStatusBarColor(getResources().getColor(R.color.black));
            }
        }
    }
}